package com.example.project10;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

/**
 * 详情页Activity
 * 用于显示推荐项的详细信息
 */
public class DetailActivity extends AppCompatActivity {

    public static final String EXTRA_ITEM = "extra_item";
    
    private TextView tvTitle;
    private TextView tvDescription;
    private TextView tvCategory;
    private TextView tvRating;
    private ImageView ivImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        // 初始化视图
        initViews();

        // 获取传递的数据
        RecommendationItem item = getIntent().getParcelableExtra(EXTRA_ITEM);
        if (item != null) {
            displayItemDetails(item);
        }

        // 设置返回按钮
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("详情页");
        }
    }

    private void initViews() {
        tvTitle = findViewById(R.id.tv_detail_title);
        tvDescription = findViewById(R.id.tv_detail_description);
        tvCategory = findViewById(R.id.tv_detail_category);
        tvRating = findViewById(R.id.tv_detail_rating);
        ivImage = findViewById(R.id.iv_detail_image);
    }

    private void displayItemDetails(RecommendationItem item) {
        tvTitle.setText(item.getTitle());
        tvDescription.setText(item.getDescription());
        tvCategory.setText("分类: " + item.getCategory());
        tvRating.setText("评分: " + String.format("%.1f", item.getRating()));
        
        // 这里可以使用图片加载库如Glide或Picasso加载图片
        // 现在先设置一个默认图片
        ivImage.setImageResource(R.drawable.ic_launcher_foreground);
    }

    @Override
    public boolean onSupportNavigateUp() {
        // 处理返回按钮点击
        onBackPressed();
        return true;
    }
}